﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxGroup_User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListtbGroupUser(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroupUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem người dùng thuộc nhóm" });
            try
            {
                tbGroupUser_Proxy pr = new tbGroupUser_Proxy();
                tbGroup_Proxy proxygroup = new tbGroup_Proxy();
                tbUsers_Proxy proxyuser = new tbUsers_Proxy();
                List<tbGroupUser> list = new List<tbGroupUser>();
                string DataResult = "";
                int totalitem = 0;
                if (keyword == "null" || keyword == "")
                {
                    list = pr.SelectAlltbGroupUser_Proxy();
                    //xuat all
                }
                else
                {
                    list = (pr.SelectNo_tbGroupUser_Proxy(keyword)).ToList<tbGroupUser>();
                }
                totalitem = list.Count;
                var list2 = list.OrderByDescending(x => x.GroupId).Skip((currentpage - 1) * numberinpage).Take(numberinpage);
                List<object> data = new List<object>();
                foreach (var item in list2)
                {
                    var user = proxyuser.GetById_tbUsers_Proxy(item.UserID).ToObject<tbUsers>();
                    var group = proxygroup.GetById_tbGroup_Proxy(item.GroupId).ToObject<tbGroup>();
                    if (user != null && group != null)
                    {
                        data.Add(new
                        {
                            id = item.Group_UserID,
                            user = user.FullName,
                            group = group.GroupName
                        });
                    }
                }



                return new JavaScriptSerializer().Serialize(new { Status = true, Data = data, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false, Data = "" });
            }
        }
        [WebMethod]
        public static string DeletetbGroupUser(int Group_UserID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroupUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa người dùng thuộc nhóm" });
            try
            {
                tbGroupUser_Proxy pr = new tbGroupUser_Proxy();
                if (pr.Delete_tbGroupUser_Proxy(Group_UserID))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string InserttbGroupUser(int GroupId, int UserID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroupUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền them mới người dùng thuộc nhóm" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbGroupUser).Name);
                tbGroupUser_Proxy pr = new tbGroupUser_Proxy();
                DataAccess.tbGroupUser a = new DataAccess.tbGroupUser();
                a.Group_UserID = 0;
                a.GroupId = GroupId;
                a.UserID = UserID;

                if (pr.Insert_tbGroupUser_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới Introduce thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string UpdatetbGroupUser(int Group_UserID, int GroupId, int UserID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroupUser.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa người dùng thuộc nhóm" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbGroupUser).Name);
                tbGroupUser_Proxy pr = new tbGroupUser_Proxy();
                DataAccess.tbGroupUser a = new DataAccess.tbGroupUser();
                a.Group_UserID = Group_UserID;
                a.GroupId = GroupId;
                a.UserID = UserID;
                if (pr.Update_tbGroupUser_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật Introduce thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
    }
}